<?php
/**
 * AES加密
 */

namespace utils;


class AES
{
    protected static $aes_key = 'JUHJ3artG68BxdJM';


    /**
     * 加密
     * @param $data 要加密的数据
     * @return bool|string   加密后的数据
     */
    public static function encrypt($data)
    {
        $str = json_encode($data);
        $data = openssl_encrypt($str, 'AES-128-CBC', self::$aes_key, OPENSSL_RAW_DATA, 'at20217211222abc');
        $data = base64_encode($data);
        return $data;
    }

    /**
     * 解密
     * @param string $str 要解密的数据
     * @return string        解密后的数据
     */
    public static function decrypt($str)
    {
        $decrypted = openssl_decrypt(base64_decode($str), 'AES-128-CBC', self::$aes_key, OPENSSL_RAW_DATA, 'at20217211222abc');
        return $decrypted = json_decode($decrypted, true);
    }
}